package com.boc.langxiang.presenter.me;

import android.os.SystemClock;

import com.boc.langxiang.contract.me.OrdersReturnProductContract;
import com.boc.langxiang.entity.request.OrdersReturnProductRequest;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.entity.response.OrdersReturnProductContent;
import com.boc.langxiang.entity.response.ProductListEntity;
import com.boc.langxiang.net.ApiService;
import com.boc.langxiang.net.HandleErrorSubscriber;
import com.boc.langxiang.net.NetClient;

import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by admin on 2016/6/24.
 */
public class OrdersReturnProductFragmentPresenter implements OrdersReturnProductContract.Presenter {

    private final ApiService mService;
    private OrdersReturnProductContract.View mView;

    private long currentCancelTime;
    private long lastCancelTime = 0l;


    public OrdersReturnProductFragmentPresenter(OrdersReturnProductContract.View view) {
        mService = NetClient.getInstance().net().create(ApiService.class);
        this.mView = view;
    }


    public void createCancelPopupWindow() {
        currentCancelTime = SystemClock.uptimeMillis();
        if (currentCancelTime - lastCancelTime > 200l) {

        }
    }

    @Override
    public void returnProduct(String page) {
        OrdersReturnProductRequest request = new OrdersReturnProductRequest();
        request.page = page;
        Observable<BaseResponse<OrdersReturnProductContent>> returnProduct = mService.returnProduct(request.params());
        returnProduct.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<OrdersReturnProductContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<OrdersReturnProductContent> response) {
                        OrdersReturnProductContent content = response.getContent();
                        if (content == null) {
                            mView.getReturnProduct(null);
                        } else {
                            List<ProductListEntity> list = content.getList();
                            mView.getReturnProduct(list);
                        }
                    }
                });
    }
}
